 // Snake.cpp: implementation of the CSnake class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "test.h"
#include "Snake.h"
#include "Section.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSnake::CSnake()
{
	this->way = RIGHT;
}

CSnake::CSnake(int count)
{
	int i =0;
	CSection *pSec;
	CPoint    point(150, 150);

	this->way = RIGHT;

	for(i = 0; i < count; i++)
	{
		pSec = new CSection();
		pSec->SetNum(i);
		pSec->SetPos(point);

		this->list.AddTail(pSec);

		if (i == 0)
		{
			this->head = point;
		}
		
		point.x	 += 10;
	}
}

void CSnake::SetHead(CPoint &point)
{
	this->head = point;
}

CPoint& CSnake::GetHead()
{
	return this->head;
}

void CSnake::EatFood()
{
	CSection *pSec = NULL;
	CPoint    point;
	
	CPoint head = this->GetHead();
	
	//move head
	switch(this->way)
	{
	case LEFT:				
		head.x -= 10;
		break;
	case RIGHT:
		head.x += 10;
		break;
	case TOP:
		head.y -= 10;
		break;
	case BOTTOM:
		head.y += 10;
		break;
	}
	
	pSec = new CSection();
	//pSec->SetNum(i);
	pSec->SetPos(head);
	
	this->list.AddHead(pSec);
	this->SetHead(head);
}

void CSnake::Move()
{
	CSection *pSec = NULL;
	CPoint    point;

	CPoint head = this->GetHead();

	//move head
	switch(this->way)
	{
		case LEFT:				
			head.x -= 10;
			break;
		case RIGHT:
			head.x += 10;
			break;
		case TOP:
			head.y -= 10;
			break;
		case BOTTOM:
			head.y += 10;
			break;
	}

	pSec = new CSection();
	//pSec->SetNum(i);
	pSec->SetPos(head);
	
	this->list.AddHead(pSec);
	this->SetHead(head);

	pSec = (CSection *)this->list.RemoveTail();
	if (pSec != NULL)
	{
		delete pSec;
	}

	TRACE("Total section: %d\n", list.GetCount());
	TRACE("head pos: x = %d, y = %d\n", head.x, head.y);
}

void CSnake::SetWay(DIRECTION dir)
{
	this->way = dir;
}

DIRECTION CSnake::GetWay()
{
	return this->way;
}


void CSnake::Draw(CDC *pDC)
{
	POSITION pos;

	CBrush brush(RGB(127, 127, 127));
	CBrush *pOldBrush;

	pOldBrush = pDC->SelectObject(&brush);

	for( pos = list.GetHeadPosition(); pos != NULL; )
	{

		CSection *pSec = (CSection *)list.GetNext(pos);

		pSec->Draw(pDC);
	}

	pDC->SelectObject(pOldBrush);

}

bool CSnake::IsOver()
{
	CPoint head = this->head;

	// catch wall
	if (head.x < 50 || head.y < 50 || (head.x + 10) > 450 || (head.y + 10) > 450)
	{
		return true;
	}

	//catch itself
	POSITION pos = list.GetHeadPosition();
	CSection *phead = (CSection *)list.GetNext(pos);

	for( ; pos!= NULL; )
	{
		CSection *pSec = (CSection *)list.GetNext(pos);

		if (phead->GetPos().x == pSec->GetPos().x &&
			phead->GetPos().y == pSec->GetPos().y)
		{
			TRACE("catch itself. x = %d, y = %d\n", phead->GetPos().x, phead->GetPos().y);
			return true;
		}
	}	

	return false;	
}

CSnake::~CSnake()
{

}
